<!--
  * 任务列表
-->
<template>
  <a-form class="smart-query-form" v-privilege="'task:query'">
    <a-row class="smart-query-form-row">
      <a-form-item label="事项名称" class="smart-query-form-item" name="memoName">
        <a-input style="width: 260px" v-model:value="queryForm.memoName" placeholder="请输入事项名称" />
      </a-form-item>
      <a-form-item label="关联项目" class="smart-query-form-item">
       <ProjectSelectTree  v-model:value="queryForm.projectId"  width="260px" />
      </a-form-item>
      <a-form-item label="事项人员" class="smart-query-form-item" name="userId">
        <DictSelect :service-id="SERVICE_ID_ENUM.USER.value" v-model:value="queryForm.userId" style="width: 200px" :showSearch="true" :allowClear="true" placeholder="事项人员" />
      </a-form-item>
      <a-form-item label="事项类型" class="smart-query-form-item" name="memoType">
        <DictSelectType :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="DICT_TYPE_ENUM.TM_MEMO_TYPE.value" v-model:value="queryForm.memoType" width="120px" placeholder="事项类型" />
      </a-form-item>
      
      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button type="primary" @click="ajaxQuery">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="add()" v-privilege="'task:add'" type="primary" size="small">
          <template #icon>
            <PlusOutlined />
          </template>
          新建事项
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.TASK.TASK_INFO" :refresh="ajaxQuery" />
      </div>
    </a-row>

    <a-table
      :scroll="{ x: 1300 }"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="id"
      :pagination="false"
      :loading="tableLoading"
      bordered
    >
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'name'">
          <a @click="detail(record.id)">{{ text }}</a>
        </template>
        <template v-if="column.dataIndex === 'taskRate'">
          <span>{{ text }}%</span>
        </template>
        <template v-if="column.dataIndex === 'planBegin'">
          <span>{{ dayjs(text).format("YYYY-MM-DD") }}</span>
        </template>
        <template v-if="column.dataIndex === 'createTime'">
          <span>{{ dayjs(text).format("YYYY-MM-DD") }}</span>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="update(record)" v-privilege="'memo:edit'" type="link">编辑</a-button>
            <a-button @click="confirmDelete(record.id)" danger v-privilege="'memo:delete'" type="link">删除</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageIndex"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>
    <MemoOperate ref="operateRef" @refresh="ajaxQuery" />
  </a-card>
</template>
<script setup lang="ts">
  import { reactive, ref, onMounted,computed } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import dayjs from 'dayjs';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { memoApi } from '/@/api/memo-api/memo-api';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { useRouter } from 'vue-router';
  import MemoOperate from './memo-operate-modal.vue';
  import { smartSentry } from '/@/lib/smart-sentry';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
  import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
  import DictSelectType from '/@/components/cache/dict-select-type.vue';
  import DictSelect from '/@/components/cache/dict-select.vue'
  import ProjectSelectTree from '/@/components/business/task/project-select-tree.vue';
  import HasUsersSelect from '/@/components/business/task/has-users-select.vue';

  // --------------------------- 企业表格 列 ---------------------------

  const columns = ref([
    {
      title: '事项名称',
      dataIndex: 'memoName',
      ellipsis: true,
    },
    {
      title: '事项类型',
      dataIndex: 'memoTypeName',
      width: 120,
      ellipsis: true,
    },
    {
      title: '关联项目',
      dataIndex: 'projectName',
      ellipsis: true,
    },
    {
      title: '人员姓名',
      dataIndex: 'userName',
      width: 120,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 120,
    },
      {title: '备注',
      dataIndex: 'remark',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 100,
    },
  ]);

  // --------------------------- 查询 ---------------------------

  const queryFormState = {
    memoName: undefined,
    memoType: undefined,
    projectId:undefined,
    userId: undefined,
    pageIndex: 1,
    pageSize: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

    let selectedRowKeys = ref([]);
    let selectedRows = ref([]);
    // 是否有选中：用于 批量操作按钮的禁用
    const hasSelected = computed(() => selectedRowKeys.value.length > 0);

    function onSelectChange(keyArray, selectRows) {
      selectedRowKeys.value = keyArray;
      selectedRows.value = selectRows;
    }

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      let responseModel = await memoApi.searchPage(queryForm);
      if(responseModel.success){
        const list = responseModel.data.data;
        total.value = responseModel.data.total;
        tableData.value = list;

         selectedRowKeys.value = [];
         selectedRows.value = [];
      }else
        message.error(responseModel.message);
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // --------------------------- 删除 ---------------------------

  function confirmDelete(taskId) {
    Modal.confirm({
      title: '确定要删除吗？',
      content: '删除后，该信息将不可恢复',
      okText: '删除',
      okType: 'danger',
      onOk() {
        del(taskId);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  async function del(taskId) {
    try {
      SmartLoading.show();
      await memoApi.delete(taskId);
      message.success('删除成功');
      ajaxQuery();
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      SmartLoading.hide();
    }
  }

  // --------------------------- 增加、修改、详情 ---------------------------
  const operateRef = ref();
  function add() {
    operateRef.value.showModal();
  }

  function update(record) {
    operateRef.value.showModal(record);
  }
  onMounted(ajaxQuery);
</script>
